/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tetris_invader.windows;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
import javax.swing.border.Border;
import tetris_invader.util.Fase;

public class InfoFrame extends JFrame {

    private JPanel painel;
    private InfoPanel painelInfo;
    private JPanel painelbotao;
    private JButton restart;
    private JButton exit;
    private Frame frame;
    private Fase fase;
    private Ouvinte ouvinte;
    private JProgressBar energy;

    public InfoFrame(Frame frame, Fase fase) {

        this.frame = frame;
        this.fase = fase;

        painel = new JPanel(new BorderLayout());
        painelbotao = new JPanel();
        painelInfo = new InfoPanel();
        restart = new JButton("Restart");
        exit = new JButton("Exit");
        ouvinte = new Ouvinte();
        energy = new JProgressBar();

        restart.addActionListener(ouvinte);
        exit.addActionListener(ouvinte);

        painelbotao.add(restart, BorderLayout.SOUTH);
        painelbotao.add(exit,BorderLayout.SOUTH);

        Border bord = BorderFactory.createEtchedBorder(Color.lightGray, Color.black);
        Border borda = BorderFactory.createCompoundBorder(bord, bord);
        Border borda2 = BorderFactory.createCompoundBorder(borda, bord);

        painelInfo.setBorder(borda2);

        painel.add(painelInfo,BorderLayout.CENTER);
        painel.add(painelbotao,BorderLayout.SOUTH);
        this.setTitle("Informacoes");
        this.setSize(300, 600);
        this.setLocation(frame.getLocation().x + frame.getWidth(), frame.getLocation().y);
        this.setResizable(false);
        this.addWindowListener(new Closer());

        this.getContentPane().add(painel);
    }

    /*TODO ideia é fazer com que a classe nave tenha uma instancia de InfoPAnel
     dai colocar oas atributos de pontos e etc so no Infopanel*/
    public void setPoints(){
    }

    public void start() {
        painelInfo.start();
    }

    public void update(){
        painelInfo.update();
    }

    public InfoPanel getInfoPanel(){
        return painelInfo;
    }

/*******INNER CLASSES**************/
    public class Ouvinte implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JButton) {
                String str = e.getActionCommand();

                if (str.equals("Restart")) {
                    int x = JOptionPane.showConfirmDialog(null, "Are you sure???\n"
                            + "Your current data will be lost!",
                            "Exit", JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.WARNING_MESSAGE);
                    if (x == JOptionPane.OK_OPTION) {
                        frame.restart();
                    }

                    
                } else if (str.equals("Exit")) {
                    int x = JOptionPane.showConfirmDialog(null, "Are you sure???\n"
                            + "Your current data will be lost!",
                            "Exit", JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.WARNING_MESSAGE);
                    if (x == JOptionPane.OK_OPTION) {
                        System.exit(3);
                    }
                }
            }
        }
    }

    public class Closer extends WindowAdapter implements WindowListener {

        @Override
        public void windowClosing(WindowEvent e) {

            int arg = e.getNewState();

            if (arg == 0) {//WindowEvent.WINDOW_CLOSED) {
                int x = JOptionPane.showConfirmDialog(null, "Are you sure???\n"
                        + "Your current data will be lost!",
                        "Exit", JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE);
                if (x == JOptionPane.OK_OPTION) {
                    System.exit(2);
                } else if (x == JOptionPane.CANCEL_OPTION) {
                    //TODO arrumar o cancelar
                }
            }
        }
    }
}
